package com.lnking.lnkingplace.controller;


import com.lnking.common.http.ReturnResult;
import com.lnking.lnkingplace.Util.UserUtil;
import com.lnking.lnkingplace.model.dto.place.PlaceCountDto;
import com.lnking.lnkingplace.model.dto.place.PlacePositionDto;
import com.lnking.lnkingplace.model.entity.PlaceInfo;
import com.lnking.lnkingplace.model.enums.ProcessStatus;
import com.lnking.lnkingplace.model.enums.UserType;
import com.lnking.lnkingplace.model.request.place.*;
import com.lnking.lnkingplace.service.IPlaceInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhangqt
 * @since 2022-08-18
 */
@Api(tags = "场所信息")
@RestController
@RequestMapping("/place-info")
public class PlaceInfoController {

    @Resource
    IPlaceInfoService placeInfoService;

    @Resource
    UserUtil userUtil;

    @ApiOperation("查询所有街道")
    @GetMapping("query-street-count")
    public ReturnResult queryStreetCount(){
        //监管
        String type = userUtil.getUser().getType();
        List<PlaceCountDto> result = null;
        if(UserType.LEADER.equals(type)
                || UserType.SUPERVISE.equals(type)){
            result = placeInfoService.queryStreetCount("");
        }
        //街道
        if(UserType.STREET.equals(userUtil.getUser().getType())){
            result = placeInfoService.queryStreetCount(userUtil.getUser().getName());
        }
        return ReturnResult.success(result);
    }

    @ApiOperation("按街道查询")
    @GetMapping("query-count")
    public ReturnResult queryCount(String street){
        return ReturnResult.success(placeInfoService.queryCount(street));
    }

    @ApiOperation("查询场所类型")
    @GetMapping("get-type")
    public ReturnResult getType(){
        return ReturnResult.success(placeInfoService.getType());
    }

    @ApiOperation("导出excel")
    @GetMapping(value = "/export")
    public ReturnResult exportExcel(PlaceListRequest params, HttpServletResponse response) throws Exception{
        placeInfoService.exportExcel(params, response);
        return ReturnResult.success();
    }

    @PostMapping("add")
    @ApiOperation("新增场所信息")
    public ReturnResult add(@Valid @RequestBody PlaceAddRequest params) throws Exception{
        placeInfoService.add(params);
        return ReturnResult.success();
    }

    @PostMapping("update")
    @ApiOperation("修改场所信息")
    public ReturnResult update(@Valid @RequestBody PlaceUpdateRequest params) throws Exception{
        placeInfoService.update(params);
        return ReturnResult.success();
    }

    @GetMapping("get-details")
    @ApiOperation("查询场所详情")
    public ReturnResult getDetails(String id){
        PlaceInfo details = placeInfoService.getDetails(id);
        return ReturnResult.success(details);
    }



    @GetMapping("get-position")
    @ApiOperation("地图查询")
    public ReturnResult getPosition(String type,String street){
        List<PlacePositionDto> placePositionDtos = placeInfoService.getPosition(type,street);
        return ReturnResult.success(placePositionDtos);
    }



    @GetMapping("list")
    @ApiOperation("查询场所列表(带分页)")
    public ReturnResult queryPageList(PlaceListRequest params){
        params.setChannel(userUtil.getUser().getChannel());
        //网格员只能查询绑定街道的
        if(UserType.STREET.equals(userUtil.getUser().getType())){
//            List<String> list = userStreetService.queryStreet(userUtil.getUser().getId());
//            params.setStreetData(list);
            params.setStreet(userUtil.getUser().getName());
//            params.setProcessAddStatus(ProcessStatus.FAILED);
        }
        return ReturnResult.success(placeInfoService.pageList(params));
    }

    @GetMapping("/management/list")
    @ApiOperation("场所处置记录")
    public ReturnResult managementPlaceList(ManagementPlaceListRequest params){
        params.setChannel(userUtil.getUser().getChannel());
        return ReturnResult.success(placeInfoService.managementList(params));
    }



    @ApiOperation("删除")
    @DeleteMapping("/{id}")
    public ReturnResult delete(@PathVariable("id") String id) {
        placeInfoService.delete(id);
        return ReturnResult.success();
    }

    @ApiOperation("修改营业状态")
    @PostMapping("/update-operating-status")
    public ReturnResult updateOperatingStatus(@RequestBody PlaceUpdateStatusRequest params){
        placeInfoService.updateOperatingStatus(params);
        return ReturnResult.success();
    }

    @ApiOperation("审核新增场所")
    @PostMapping("/update-process-add-status")
    public ReturnResult updateProcessAddStatus(@RequestBody PlaceUpdateProcessAddStatus params){
        placeInfoService.updateProcessAddStatus(params);
        return ReturnResult.success();
    }


}

